﻿//初始化
$(document).ready(function(){
	if ($("#protectionhr").length > 0){
		if(window.localStorage){
			var articlejsonobject = localStorage["articlejsonobject_" + articledetailid];
			if (typeof(articlejsonobject) != "undefined"){
				var article = JSON.parse(articlejsonobject);
				$(".protectionpanel").html("");
				if (typeof(article.digest) != "undefined"){
					$("#protectionhr").hide();
					$(".protectionpanel").append("<div class='articleAbstract'><b>摘要：</b>" + article.digest + "</div>");
				}
				$(".protectionpanel").append("<div class='articleContents'>" + article.contents + "</div>");
				$(".protectionpanel").removeAttr("class");
				getclicknumber();
			} else {
				$(".protectionpanel").show();
				if ($("#vcode").val() != ""){
					$("#vcode").val("");
			        changevocde();
				}
			}
		} else {
			$(".protectionpanel").show();
			if ($("#vcode").val() != ""){
				$("#vcode").val("");
		        changevocde();
			}
		}
	} else {
		getclicknumber();
	}
});

//获取点击数
function getclicknumber(){
	if (readCookie("articleclicknum_" + articledetailid) == null){
		$.ajax({  
			type: "get",      
			url: "/article/getclicknumber?id=" + articledetailid, 
			cache: false,
			success: function(data){  
				$("#clicknum").html(parseInt(data));
			},
			error: function(){   
				$("#clicknum").html("error");
			} 
		});
	} else {
		$("#clicknum").html(parseInt(readCookie("articleclicknum_" + articledetailid)));
	}
}

//回车事件
function keydownevent(){
	if(event.keyCode == 13) passcontents();
}

//查看加密内容
function passcontents(){
	if(checknull("protectionpassword", "密码") && checknull("vcode", "验证码")){
		var result;
		$.ajax({  
			type: "post",      
			url: "/article/getcontents", 
	        data: {id:articledetailid,password:$("#protectionpassword").val(),vcode:$("#vcode").val()},
			cache: false,
	        dataType: "json",
			success: function(data){
	        	switch (parseInt(data.state)){
					case -1:
						alert("验证码加载失败");
						window.location.replace(location.href);
						break;
					case -2:
						alert("验证码错误，请重新输入。");
						$("#vcode").val("");
				        changevocde();
						break;
					case 0:
						alert("密码错误，请重新输入。");
						$("#vcode").val("");
				        changevocde();
						$("#protectionpassword").focus();
						break;
					default:
						$(".protectionpanel").html("");
						if (typeof(data.digest) != "undefined"){
							$("#protectionhr").hide();
							$(".protectionpanel").append("<div class='articleAbstract'><b>摘要：</b>" + data.digest + "</div>");
						}
						$(".protectionpanel").append("<div class='articleContents'>" + data.contents + "</div>");
						$(".protectionpanel").removeAttr("class");
						if(window.localStorage) localStorage["articlejsonobject_" + articledetailid] = JSON.stringify(data);
						getclicknumber();
						break;
				}
			},
			error: function(){   
				alert("error");
			} 
		});
	}
}